package com.kelvin.paopao.widgets;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kelvin.kelvinlib.bitmap.BitmapUtil;
import com.kelvin.kelvinlib.utils.LogUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.AddImageAdapter;
import com.kelvin.paopao.module.person.photoset.PhotoSetActivity;
import com.kelvin.paopao.utils.DialogMaker;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

/**
 * Created by Administrator on 2017/5/14.
 */
public class PublishImageEditView extends RecyclerView {

    private AddImageAdapter adapter;
    private int maxCount;
    private View headView;
    private LinearLayoutManager manager;

    private boolean isCompressing;

    private List<String> base64s;

    public PublishImageEditView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        base64s = new ArrayList<>();
    }

    public PublishImageEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public void create(final Context context) {
        adapter = new AddImageAdapter(context);
        headView = LayoutInflater.from(context).inflate
                (R.layout.img_head_add_view, (ViewGroup) getParent(), false);

        headView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listner != null) {
                    listner.addImage();
                }
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                removePositionImage(position);
                //进入图片浏览页面

                PhotoSetActivity.launch(context, adapter.getData(), position);
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                showDeleteDialog(position);
            }
        });
        manager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, true);
        setLayoutManager(manager);
        setAdapter(adapter);
        adapter.addHeaderView(headView);
        adapter.setNewData(null);
    }

    public void addImageView(String imgPath) {
        if (!isCompressing) {
            adapter.addData(imgPath);
            //使用鲁班压缩图片
            Luban.get(getContext())
                    .load(new File(imgPath))                     //传人要压缩的图片
                    .putGear(Luban.THIRD_GEAR)      //设定压缩档次，默认三挡
                    .setCompressListener(new OnCompressListener() { //设置回调
                        @Override
                        public void onStart() {
                            isCompressing = true;
                            DialogMaker.showProgressDialog(getContext(), "图片压缩中~~");
                        }

                        @Override
                        public void onSuccess(File file) {
                            isCompressing = false;
                            String base64 = BitmapUtil.fileToBase64(file);
                            base64.replace("+", "%2B");
                            base64s.add(base64);
                            DialogMaker.diamiss();
                        }

                        @Override
                        public void onError(Throwable e) {
                            DialogMaker.diamiss();
                            isCompressing = false;
                        }
                    }).launch();
            if (adapter.getData().size() >= maxCount) {
                adapter.removeAllHeaderView();
            } else {
                LogUtil.d("getHeaderLayoutCount:" + adapter.getHeaderLayoutCount());
                if (adapter.getHeaderLayoutCount() == 0) {
                    adapter.addHeaderView(headView);
                }
            }
        }
    }

    public interface AddImgListner {
        void addImage();
    }

    private AddImgListner listner;

    public void setListener(AddImgListner listener) {
        this.listner = listener;
    }

    public void setMaxCount(int count) {
        this.maxCount = count;
    }

    /**
     * 获取当前位置上的图片base64
     *
     * @param position
     * @return
     */
    public String getPositionImage(int position) {
        if (adapter.getData().size() >= position + 1) {
            return base64s.get(position);
        } else {
            return null;
        }
    }

    /**
     * 移除当前位置上的图片
     *
     * @param position
     * @return
     */
    public void removePositionImage(int position) {
        if (adapter.getHeaderLayoutCount() == 0) {
            adapter.addHeaderView(headView);
        }
        adapter.remove(position);
    }

    /**
     * 是否还在压缩中
     *
     * @return
     */
    public boolean isCompressing() {
        return isCompressing;
    }

    private void showDeleteDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("提示").setMessage("是否确定删除该图片").setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                removePositionImage(position);
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();
    }
}
